package Controllers

import (
	"encoding/base64"
	"io"
	"net/http"
	"yz-go/Common"
	"yz-go/Interface"
	"yz-go/Logic"

	"github.com/gin-gonic/gin"
)

func Distinguish(c *gin.Context) {

	// var res Common.ResData

	req := c.Request //  此方法更加底层
	imageUrl := c.PostForm("image_url")

	var imageBase string

	if imageUrl == "" {
		file, _, err := req.FormFile("image_file")
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": Common.ERROR,
				"msg":  err,
				"data": nil,
			})
			return
		}

		bytes, err := io.ReadAll(file)

		imageBase = base64.StdEncoding.EncodeToString(bytes)
	}

	// path := "https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/general/GeneralBasicOCR/GeneralBasicOCR1.jpg"

	// res = Logic.PTDistinguish(imageUrl, &imageBase)

	// if res.Code == Common.SUCCESS {
	// 	c.JSON(http.StatusOK, gin.H{
	// 		"code": Common.SUCCESS,
	// 		"msg":  "success",
	// 		"data": res,
	// 	})
	// 	return
	// }

	// 方式二：【接口的方式去调用】
	resv2 := &Logic.GeneralBasic{} // 方式一
	// var resv2 Logic.GeneralBasic // 方式二

	input := map[string]string{"imageUrl": imageUrl, "imageBase": imageBase}
	// v2, errstr := resv2.TXOCR(&input)

	var aa Interface.OCRIntf
	aa = resv2 // 实现接口
	v2, errstr := aa.TXOCR(&input)

	if errstr == nil {
		c.JSON(http.StatusOK, gin.H{
			"code": Common.SUCCESS,
			"msg":  "success",
			"data": v2,
		})
		return
	}
}
